#include<stdio.h>
#include <stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include <string.h>
#include<netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#define SERVPORT 5555
#define DEST_IP "127.0.0.1"

int main(int argc, char **argv)
{   
    struct sigaction sigpiple;
    //SIGPIPE ignore
    sigpiple.sa_handler =SIG_IGN; 
    sigaction(SIGPIPE, &sigpiple, NULL);
    
    struct linger		ling;
    ling.l_onoff = 1;		/* cause RST to be sent on close() */
	ling.l_linger = 0;
    
    int sockfd,sock_dt;
    printf("#####################################################\n");
    printf("socket test      by pafone   19th,April,2009\n");
    printf("#####################################################\n");
    struct sockaddr_in my_addr;//local ip info
    struct sockaddr_in dest_addr; //destnation ip info
    
    struct sigaction mysigpiple;
    
    //程序启动的时候信号重新注册对应功能
    mysigpiple.sa_flags = 0;
    mysigpiple.sa_handler =SIG_IGN;  
    sigaction(SIGPIPE,&mysigpiple, NULL);
    
    if(argc != 3)
    {
        printf("useage:client 127.0.0.1 5555 \n");
        return -1;
    }
    
    int destport = atoi(argv[2]);
   //create connetct one times
    for(int m=0;m<1;m++)
    {  
         if(-1 == (sockfd = socket(AF_INET,SOCK_STREAM,0)) )
        {
            perror("error in create socket\n");
            exit(0);
        }
        dest_addr.sin_family = AF_INET;
        dest_addr.sin_port = htons(destport);
        dest_addr.sin_addr.s_addr = inet_addr(argv[1]);
        memset(&dest_addr.sin_zero,0,8);
        
        //阻塞connect
        if(-1 == connect(sockfd,(struct sockaddr*)&dest_addr,sizeof(struct sockaddr)))
        {
            perror("connect error\n");
            printf("=================errno=%d..\n",errno);
            exit(0);
        }
       setsockopt(sockfd, SOL_SOCKET, SO_LINGER, &ling, sizeof(ling));
        // send data
        char buf[1024];
        int n_send_len=0;
        //send mang data 
        for(int i=0;i<10;i++ )
        {   
            memset(buf,'\0',1024);
            sprintf(buf,"client:%d",i);
            //阻塞与发送过程，可以忽略不计
            // 直接抛出异常 Broken 系统来处理。。。。
            n_send_len = send(sockfd,buf,strlen(buf),0);
            printf(" client has send for %d bytes data=%s\n",n_send_len,buf);
            //发送字节数
            if(n_send_len >0)
            {
              // printf(" client has send for %d bytes data=%s\n",n_send_len,buf);
            }else if( n_send_len == -1)
            {   //发生-1情况
                //如果服务端挂掉并不返回-1 而是抛出管道信号
                //errno.32 is: Broken pipe
                printf("=================errno=%d..\n",errno);
               if(errno==EINTR) continue;
            }else
            {
                
            }
            //work 
            sleep(2);
           
        }
        close(sockfd);
        printf("client(%d) socketid=%d send close \n",m,sockfd);
        sockfd =0;
    }
}
